using IQIGame.Onigao.GamePlay;

namespace IQIGame.Onigao.Logic
{
    /// <summary>
    /// 改变电梯楼层的激活或失效状态
    /// 参数1：1：tag（0表示自己，注意tag对应的实体必须是电梯）
    /// 参数2：楼层|状态|楼层|状态...（状态：0：失效，1：激活）
    /// </summary>
    public class ExecSwitchElevatorFloor : BaseServiceExecution
    {
        public override void Execute(LevelRegion region, LevelPlayer player)
        {
            var config = _config as ExecParamSwitchElevatorFloor;
            if (!config.tagOrMcid.TryGetValue(triggerEntity, out int tagOrMcid))
            {
                LogicLog.LogError("无法获取到tagOrMcid.");
                return;
            }
            LevelEntity elevatorEntity = tagOrMcid == 0 ? triggerEntity : region.GetEntityByTagOrMcid(tagOrMcid);
            EntityParamElevator elevatorParam = elevatorEntity?.GetDynamicParam(EntityDynamicParamType.Elevator) as EntityParamElevator;
            if (elevatorParam == null)
            {
                LogicLog.LogError("没有找到电梯实体。TagOrMcid：" + tagOrMcid);
                return;
            }
            foreach (var kv in config.floors)
            {
                int floor = kv.Key;
                bool isActive = kv.Value;
                EntityPropElevator elevatorProp = elevatorEntity.GetDynamicProperty(EntityDynamicPropertyType.Elevator) as EntityPropElevator;
                if (floor < 0 || floor >= elevatorProp.floorActiveList.Length)
                {
                    LogicLog.LogError("楼层参数错误。楼层：" + floor);
                    return;
                }
                elevatorProp.floorActiveList[floor] = isActive;
            }
        }
    }
}
